package ThePlaneWar;

import obj.*;
import utils.GameUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameWin extends JFrame{

    //游戏状态 0未开始 1游戏中 2暂停 3通关失败 4通关成功

    public static int state = 0;
    //游戏得分
    public static int score = 0;

    Image offScreenImage = null;
    int width = 600;
    int height = 800;

    //游戏的重绘次数
    int count = 1;
    //敌机出现的数量
    int enemyCount = 0;

    //背景图对象
    BgObj bgObj = new BgObj(GameUtils.bgImg,0,-200,2);
    //我方飞机对象
    public PlaneObj planeObj = new PlaneObj(GameUtils.palneImg,290,550,20,30,0,this);

    //敌方boss对象
    public BossObj boss1Obj = null;

    public void launch() {
        //设置窗口可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(width, height);
        //设置窗口位置，让它在屏幕的中央
        this.setLocationRelativeTo(null);
        //设置窗口标题
        this.setTitle("飞机大战");

        GameUtils.gameObjList.add(bgObj);
        GameUtils.gameObjList.add(planeObj);

        //鼠标单击事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 1 && state == 0) {
                    state = 1;
                    repaint();
                }
            }
        });

        //游戏暂停
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == 32) {
                    switch (state) {
                        case 1:
                            state = 2;
                            break;
                        case 2:
                            state = 1;
                            break;
                    }
                }
            }
        });

        //重复绘制
        while (true) {
            if (state == 1) {
                createObj();
                repaint();
            }

            try {
                Thread.sleep(25);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //重写paint方法
    @Override
    public void paint(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = createImage(width,height);
        }
        Graphics gImage = offScreenImage.getGraphics();
        gImage.fillRect(0,0,width,height);

        if (state == 0) {
            gImage.drawImage(GameUtils.bgImg, 0, 0, null);
            gImage.drawImage(GameUtils.bg1Img, 40, 20, null);
            gImage.setColor(Color.yellow);
            GameUtils.drawWord(gImage,"点击开始游戏",Color.yellow,40,170,580);
        }

        if (state == 1) {
            GameUtils.gameObjList.addAll(GameUtils.explodeObjList);
            //运行中
            for (int i = 0; i < GameUtils.gameObjList.size(); i++) {
                GameUtils.gameObjList.get(i).paintSelf(gImage);
            }
            GameUtils.gameObjList.removeAll(GameUtils.removeList);
        }

        if (state == 3) {
            //失败
            gImage.drawImage(GameUtils.explode1Img, planeObj.getX(),planeObj.getY(),null);
            GameUtils.drawWord(gImage,"GAME OVER",Color.RED,50,180,400);
        }

        if (state == 4) {
            //通过成功
            GameUtils.drawWord(gImage,"游戏通关",Color.green,50,190,400);
        }
        GameUtils.drawWord(gImage,score+"分",Color.green,40,30,100);

        g.drawImage(offScreenImage,0,0,null);
        count++;
        System.out.println(GameUtils.gameObjList.size());
    }

    void createObj () {
        //我方子弹
        if (count % 15 == 0) {
            GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg, planeObj.getX() + 50, planeObj.getY() - 10, 14, 29, 5, this));
            GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size() - 1));
        }

        //敌方子弹
        if (count % 15 == 0 && boss1Obj != null) {
            GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImg,boss1Obj.getX() + 200,boss1Obj.getY() + 200,15,25,5,this));
            GameUtils.gameObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size() - 1));
        }

        //敌方飞机
        if (count % 15 == 0) {
            GameUtils.enemyObjList.add(new EnemyObj(GameUtils.enemtImg,(int)(Math.random()*6)*100,0,49,36,5,this));
            GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.enemyObjList.size() - 1));
            enemyCount++;
        }

        //游戏规则,先出现100个小兵才会出现boss
        if (enemyCount > 100 && boss1Obj == null) {
            boss1Obj = new BossObj(GameUtils.boss1Img,250,35,155,100,5,this);
            GameUtils.gameObjList.add(boss1Obj);
        }
    }

    public static void main(String[] args) {
        new GameWin().launch();
    }
}
